package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.repository.KangZhenTuRepository;
import com.css.fxfzypg.modules.baseData.entity.KangZhenTu;
import com.css.fxfzypg.modules.baseData.service.KangZhenTuService;
import com.css.fxfzypg.modules.baseData.queryParams.KangZhenTuQueryParams;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class KangZhenTuServiceImpl implements KangZhenTuService {
    @Resource
    KangZhenTuRepository kangZhenTuRepository;

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public KangZhenTuVO updateKangZhenTu(ElevationVO elevationVO) {
//
//        ElevationVO elevationVO1 = elevationRepository.updateElevation(elevationVO);
//
//        return elevationVO1;
//    }

    @Override
    @Transactional
    public KangZhenTu saveKangZhenTu(KangZhenTu kangZhenTu) {
        kangZhenTu.setKztId(UUIDGenerator.getUUID());
        kangZhenTu.setKztStatus(YNEnum.Y.toString());
        kangZhenTu.setKztCreatetime(PlatformDateUtils.getCurrentTimestamp());
        return kangZhenTuRepository.saveKangZhenTu(kangZhenTu);
    }

    @Override
    public JSONObject getAllKangZhenTu(KangZhenTuQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(kangZhenTuRepository.getAllKangZhenTuParams(queryParams));
    }

    @Override
    public KangZhenTu getKangZhenTuById(String id) {
        KangZhenTu kangZhenTu = kangZhenTuRepository.getKangZhenTuById(id);
        return kangZhenTu;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteKangZhenTus(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        kangZhenTuRepository.deleteKangZhenTusById(ids,timestamp);
    }

    @Override
    public List<KangZhenTu> getAll() {

        return kangZhenTuRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<KangZhenTu> kangZhenTus = FileUtil.importExcel(file, KangZhenTu.class);

        for(int i = 0 ; i<kangZhenTus.size();i++){
            kangZhenTus.get(i).setKztId(UUIDGenerator.getUUID());
            kangZhenTus.get(i).setKztStatus(YNEnum.Y.toString());
            kangZhenTus.get(i).setKztCreatetime(PlatformDateUtils.getCurrentTimestamp());

        }
        kangZhenTuRepository.importExcel(kangZhenTus);
    }

    @Override
    public int getHouseTotalByLonAndLat(String lon, String lat,Boolean flag) {
        return kangZhenTuRepository.getHouseTotalByLonAndLat(lon,lat,flag);
    }

    @Override
    public int getByGeom(String geom, Boolean flag) {
        return kangZhenTuRepository.getByGeom(geom,flag);
    }
}
